from django.http import JsonResponse
from django.shortcuts import render
from comment.logcode import *

from utils.tokens import *

not_token_url = ["/user/login", "/user/"]


def jwt_middleware(get_response):
    def middleware(request):
        token = request.GET.get("token")
        if request.path not in not_token_url:
            if token is None:
                return JsonResponse({"code": LOGIN_ERROR, "message": "params is not ful"})
            elif not check_token(token):
                return JsonResponse({"code": LOGIN_ERROR, "message": "token is timeout"})
        response = get_response(request)
        print(request.path)
        return response
    
    return middleware
